package com.heima.homeservicemanagementsystem.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

/**
 * @Description
 * @Author susu
 * @Date 2024/11/25
 */
public class PasswordUtils {
    /**
     * 加密
     *
     * @param salt
     * @param password
     * @return java.lang.String
     * @author xinggang
     * @create 2024/11/25
     **/

    public static String encrypt(String salt, String password) {
        byte[] byteKey = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(),
                salt.getBytes()).getEncoded();
        SymmetricCrypto aes = SecureUtil.aes(byteKey);
        // 加密
        return aes.encryptBase64(password);

    }

    /**
     * 解密
     *
     * @param salt
     * @param password
     * @return java.lang.String
     * @author xinggang
     * @create 2024/11/25
     **/

    public static String decryptStr(String salt, String password) {
        byte[] byteKey = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(),
                salt.getBytes()).getEncoded();
        SymmetricCrypto aes = SecureUtil.aes(byteKey);
        // 加密
        return aes.decryptStr(password);

    }

//    public static void main(String[] args) {
//        String s1 = RandomUtil.randomString(16);
//        String encrypt = encrypt(s1, "123456");
//        System.out.println(encrypt);
//        String s = decryptStr(s1, encrypt);
//        System.out.println(s);
//
//        System.out.println(s1);
//    }
}
